package me.send.produce.data.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.send.business.base.Pager;
import me.send.data.service.pojo.BatchWithdrawOrder;
import me.send.data.service.pojo.BatchWithdrawRecord;
import me.send.data.service.pojo.WithdrawOrder;
import me.send.data.service.pojo.WithdrawRecord;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.data.service.service.TreeService;
import me.send.data.service.service.WithdrawService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "broker")
public class WithdrawController {

	@Autowired
	private WithdrawService withdrawService;

	@Autowired
	private TreeService treeService;


	// 查询提现订单列表
	@RequestMapping(value = "broker/withdraw/list")
	public ModelAndView withdraw_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/broker/withdraw_order");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "broker/broker/withdraw/list"));
		modelAndView.addObject("module", "经纪人提现列表");
		return modelAndView;
	}
	
	// 查询提现订单列表
	@RequestMapping(value = "broker/batch/withdraw/detail")
	public ModelAndView batch_withdraw_detail(HttpServletRequest request,String batchOrderNo) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/broker/batch_withdraw_order_detail");
		BatchWithdrawRecord batch=withdrawService.queryBatchWithdrawOrderByOrderNo(batchOrderNo);
		modelAndView.addObject("batch",batch);
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "broker/broker/batch/withdraw/list"));
		modelAndView.addObject("module", "经纪人批量付款详情");
		return modelAndView;
	}
	
	// 查询批量提现付款订单列表
	@RequestMapping(value = "broker/batch/withdraw/list")
	public ModelAndView batch_withdraw_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/broker/batch_withdraw_order");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "broker/broker/batch/withdraw/list"));
		modelAndView.addObject("module", "经纪人批量付款列表");
		return modelAndView;
	}

	
	@RequestMapping(value="/withdraw/list/batchNo", method=RequestMethod.GET)  
	@ResponseBody  
	public DatatablesViewPage<WithdrawOrder> withdraw_list_by_batchNo(HttpServletRequest request){  
//获取分页控件的信息  
		String start = request.getParameter("start");  
		String length = request.getParameter("length");  
		
		String batchOrderNo = request.getParameter("batchOrderNo"); 
		
		Pager page=new Pager();
		Integer row=Integer.parseInt(length);
		page.setRows(row);
		Integer startInt=Integer.parseInt(start);
		Integer p=(startInt/row)+1;
		page.setPage(p);
		
		//随便组织的查询结果  
		
		List<WithdrawOrder> list=withdrawService.list(batchOrderNo, page);
		Integer count=withdrawService.count(batchOrderNo);

		
		DatatablesViewPage<WithdrawOrder> view = new DatatablesViewPage<WithdrawOrder>();  
		view.setiTotalDisplayRecords(count);  
		view.setiTotalRecords(count);  
		
		view.setAaData(list);  
		return view;  
	}  
	
	@RequestMapping(value="/batch/withdraw/list", method=RequestMethod.GET)  
	@ResponseBody  
	public DatatablesViewPage<BatchWithdrawOrder> batchWithdrawList(HttpServletRequest request){  
//获取分页控件的信息  
		String start = request.getParameter("start");  
		String length = request.getParameter("length");  
//获取前台额外传递过来的查询条件  
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		
		String create_start_time = request.getParameter("create_start_time"); 
		String create_end_time = request.getParameter("create_end_time");  
		String latest_start_time = request.getParameter("latest_start_time"); 
		String latest_end_time = request.getParameter("latest_end_time");  
		String batchOrderNo = request.getParameter("batchOrderNo");  
		String status = request.getParameter("status");  
		Integer paramStatus=null;
		if(batchOrderNo!=null&&"".equals(batchOrderNo.trim())){
			batchOrderNo=null;
		}
		if(status!=null&&"".equals(status.trim())){
			status=null;
		}else{
			paramStatus=Integer.parseInt(status);
		}
		if(create_start_time!=null&&!"".equals(create_start_time.trim())&&create_end_time!=null&&!"".equals(create_end_time.trim())){
			Date startDate=null;
			Date endDate=null;
			try {
				startDate=sdf.parse(create_start_time);
				create_start_time=format.format(startDate);
				endDate=sdf.parse(create_end_time);
				create_end_time=format.format(endDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			create_start_time = null; 
			create_end_time = null; 
		}
		
		
		if(latest_start_time!=null&&!"".equals(latest_start_time.trim())&&latest_end_time!=null&&!"".equals(latest_end_time.trim())){
			Date startDate=null;
			Date endDate=null;
			try {
				startDate=sdf.parse(latest_start_time);
				latest_start_time=format.format(startDate);
				endDate=sdf.parse(latest_end_time);
				latest_end_time=format.format(endDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			latest_start_time = null; 
			latest_end_time = null; 
		}
		
		Pager page=new Pager();
		Integer row=Integer.parseInt(length);
		page.setRows(row);
		Integer startInt=Integer.parseInt(start);
		Integer p=(startInt/row)+1;
		page.setPage(p);
		
		//随便组织的查询结果  
		
		List<BatchWithdrawOrder> list=withdrawService.batch_list(batchOrderNo, paramStatus, create_start_time, create_end_time, page, latest_start_time, latest_end_time);
		Integer count=withdrawService.batch_count(batchOrderNo, paramStatus, create_start_time, create_end_time,latest_start_time, latest_end_time);
		
		
		DatatablesViewPage<BatchWithdrawOrder> view = new DatatablesViewPage<BatchWithdrawOrder>();  
		view.setiTotalDisplayRecords(count);  
		view.setiTotalRecords(count);  
		
		view.setAaData(list);  
		return view;  
	}  
	@RequestMapping(value="withdraw/list", method=RequestMethod.GET)  
    @ResponseBody  
    public DatatablesViewPage<WithdrawOrder> datatablesTest(HttpServletRequest request){  
//获取分页控件的信息  
        String start = request.getParameter("start");  
                String length = request.getParameter("length");  
//获取前台额外传递过来的查询条件  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start_time = request.getParameter("start_time"); 
        String end_time = request.getParameter("end_time");  
        String phone = request.getParameter("phone");  
        String status = request.getParameter("status");  
        String amount = request.getParameter("amount");  
        Double paramAmount =null;
        if(amount!=null&&"".equals(amount.trim())){
        	amount=null;
        }else{
        	paramAmount = Double.parseDouble(amount);
        }
        Integer paramStatus=null;
        if(phone!=null&&"".equals(phone.trim())){
        	phone=null;
        }
        if(status!=null&&"".equals(status.trim())){
        	status=null;
        }else{
        	paramStatus=Integer.parseInt(status);
        }
        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
        	Date startDate=null;
        	Date endDate=null;
        	try {
        		startDate=sdf.parse(start_time);
        		start_time=format.format(startDate);
        		endDate=sdf.parse(end_time);
        		end_time=format.format(endDate);
        	} catch (ParseException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        	}
        }else{
        	start_time=null;
        	end_time=null;
        }
        
        Pager page=new Pager();
        Integer row=Integer.parseInt(length);
        page.setRows(row);
        Integer startInt=Integer.parseInt(start);
        Integer p=(startInt/row)+1;
        page.setPage(p);
        
                //随便组织的查询结果  
        List<WithdrawOrder> list=withdrawService.list(phone,paramStatus,paramAmount,start_time,end_time,page); 
        Integer count=withdrawService.count(phone,paramStatus,paramAmount,start_time,end_time);
          
        DatatablesViewPage<WithdrawOrder> view = new DatatablesViewPage<WithdrawOrder>();  
        view.setiTotalDisplayRecords(count);  
        view.setiTotalRecords(count);  
          
        view.setAaData(list);  
        return view;  
    }  
	
	
	/**
	 * 审核
	 * @param brokerids
	 * @return
	 */
	@RequestMapping(value = "withdraw/allow")
	@ResponseBody
	public Map<String,Object> allow(String withdrawids) {
		return withdrawService.allow(withdrawids);
	}
	/**
	 * 已对账
	 * @param brokerids
	 * @return
	 */
	@RequestMapping(value = "withdraw/recon")
	@ResponseBody
	public Map<String,Object> recon(String withdrawids) {
		return withdrawService.recon(withdrawids);
	}
	
	@RequestMapping(value="/request/alipay/batch",produces = "text/html;charset=UTF-8")
	public ModelAndView requestAliBatchPay(String batchOrderNo,HttpServletRequest request,HttpServletResponse response,Model model) throws Exception{
		
		ModelAndView modelAndView = new ModelAndView("stage/broker/alipay_batch");
		BatchWithdrawRecord record=withdrawService.queryBatchWithdrawOrderByOrderNo(batchOrderNo.trim());
		List<WithdrawRecord> orders=withdrawService.queryWithdrawOrdersByBatchNo(batchOrderNo.trim());
		StringBuffer detail_data=new StringBuffer();
		for(int i=0;i<orders.size();i++){
			WithdrawRecord order=orders.get(i);
			detail_data.append(order.getWithdrawOrderNo());
			detail_data.append("^");
			detail_data.append(order.getWithdrawAccountNo());
			detail_data.append("^");
			detail_data.append(order.getBrokerName());
			detail_data.append("^");
			detail_data.append(order.getWithdrawAmount().doubleValue());
			detail_data.append("^");
			detail_data.append(order.getWithdrawDesc()==null?"经纪人（"+order.getBrokerName()+"）提现-"+order.getWithdrawOrderNo()+"":order.getWithdrawDesc()+"【呼送科技经纪人报酬结算】");
			if(i<orders.size()-1){
				detail_data.append("|");
			}
		}
		System.out.println(detail_data.toString());
		model.addAttribute("batch_no", batchOrderNo);
		model.addAttribute("batch_fee", record.getBatchFee().doubleValue()+"");
		model.addAttribute("batch_num", record.getBatchNum()+"");
		model.addAttribute("detail_data", detail_data.toString());
		return modelAndView;
		
	}
	
}
